package dao.mysql;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import modelInterfaces.AbstractModelFactory;
import modelInterfaces.IEventType;
import dao.DAOException;
import dao.IEventTypeDAO;


/**
 *
 * @author salvador
 */
public class EventTypeMySqlDAO implements IEventTypeDAO {

    private String PROPERTIES_FILE_PATH = "config/MySqlDAO.props";
	
    private String TABLE_NAME = "event_type";
	private String ID_COLUMN_NAME = "id";
    private String NAME_COLUMN_NAME = "name";
	
	
	public EventTypeMySqlDAO()
    {
    	Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(PROPERTIES_FILE_PATH));
			TABLE_NAME = 		prop.getProperty("EVENT_TYPE_TABLE_NAME") != null ?			prop.getProperty("EVENT_TYPE_TABLE_NAME") :			this.TABLE_NAME;
	        ID_COLUMN_NAME = 	prop.getProperty("EVENT_TYPE_ID_COLUMN_NAME") != null ?		prop.getProperty("EVENT_TYPE_ID_COLUMN_NAME") :		this.ID_COLUMN_NAME;
	        NAME_COLUMN_NAME = 	prop.getProperty("EVENT_TYPE_NAME_COLUMN_NAME") != null ?	prop.getProperty("EVENT_TYPE_NAME_COLUMN_NAME") :	this.NAME_COLUMN_NAME;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
		
	public void create(IEventType eventType) throws DAOException {
        MySqlDBConnection.getInstance().executeUpdate("INSERT INTO "+ TABLE_NAME +"("+ NAME_COLUMN_NAME +") VALUES('"+eventType.getName()+"')");
    }

    public IEventType retrieve(int id) throws DAOException {
        ResultSet rs = MySqlDBConnection.getInstance().executeQuery("SELECT * FROM "+ TABLE_NAME +" WHERE "+ ID_COLUMN_NAME +"='"+id+"'");
        IEventType eventType = null;
        try
        {
            while (rs.next())
            {
                eventType = AbstractModelFactory.getModelFactory().getEventType();
                eventType.setId(rs.getInt(ID_COLUMN_NAME));
                eventType.setName(rs.getString(NAME_COLUMN_NAME));
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
    	return eventType;
    }

    public List<IEventType> retrieveAll() throws DAOException
    {
        ResultSet rs = MySqlDBConnection.getInstance().executeQuery("SELECT * FROM "+ TABLE_NAME);
        List<IEventType> eventTypeList = new ArrayList<IEventType>();
        try
        {
            while (rs.next())
            {
                IEventType eventType = AbstractModelFactory.getModelFactory().getEventType();
                eventType.setId(rs.getInt(ID_COLUMN_NAME));
                eventType.setName(rs.getString(NAME_COLUMN_NAME));
                eventTypeList.add(eventType);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
    	return eventTypeList;
    }
    
    public void delete(IEventType eventType) throws DAOException {
    	// TODO [programador] Auto-generated method stub
    }

    public void update(IEventType eventType) throws DAOException {
    	// TODO [programador] Auto-generated method stub
    }

}
